package com.vercon.moensmart.utils;

import android.content.Context;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.vercon.common.room.entry.AppDataRoomInfo;
import com.vercon.common.utils.LogUtils;
import com.vercon.moensmart.fragment.AppFragment;
import com.vercon.moensmart.mode.AppMode;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class DefaultMenuUtil {
    /**
     * 默认图标的，是否隐藏功能处理
     */
    public static void default_menu(Context context){
       // PreferenceUtils.setData(PreferenceUtils.default_menu,null);
        //添加菜单功能
        String menuData = PreferenceUtils.getData(PreferenceUtils.default_menu);
        LogUtils.e("default_menu="+menuData);
        List<Integer> appList = null;
        if (!TextUtils.isEmpty(menuData)) {
            try {
                appList = new Gson().fromJson(menuData, new TypeToken<List<Integer>>() {
                }.getType());
            }catch (Exception e){//以前版本，保存了本地数据，清空以前数据，按照最新的数据结构来
                PreferenceUtils.setData(PreferenceUtils.default_menu,null);
            }
        }
        if (appList == null){
            appList = new ArrayList<>();
            List<AppMode> list = getAppModes(context);//默认所有的功能便签
            LogUtils.e("list="+list.toString());
            for (int i = 0;i < list.size();i++){
                appList.add(list.get(i).getId());
            }
        }
        LogUtils.e("appList="+appList.toString());
        PreferenceUtils.setData(PreferenceUtils.default_menu, new Gson().toJson(appList));
    }
    public static void delete_menu(int id){
        //添加菜单功能
        String menuData = PreferenceUtils.getData(PreferenceUtils.default_menu);
        LogUtils.e("default_menu="+menuData);
        List<Integer> appList = null;
        if (!TextUtils.isEmpty(menuData)) {
            try {
                appList = new Gson().fromJson(menuData, new TypeToken<List<Integer>>() {
                }.getType());
            }catch (Exception e){//以前版本，保存了本地数据，清空以前数据，按照最新的数据结构来
                PreferenceUtils.setData(PreferenceUtils.default_menu,null);
            }
        }
        if (appList != null && appList.size() > 0){
            for (int i = 0; i < appList.size();i++){
                if (appList.get(i) == id){
                    appList.remove(i);
                }
            }
            PreferenceUtils.setData(PreferenceUtils.default_menu, new Gson().toJson(appList));
        }

    }

    /**
     * 添加功能模块
     * @param id
     */
    public static void add_menu(int id){
        //添加菜单功能
        String menuData = PreferenceUtils.getData(PreferenceUtils.default_menu);
        LogUtils.e("default_menu="+menuData);
        List<Integer> appList = null;
        if (!TextUtils.isEmpty(menuData)) {
            try {
                appList = new Gson().fromJson(menuData, new TypeToken<List<Integer>>() {
                }.getType());
            }catch (Exception e){//以前版本，保存了本地数据，清空以前数据，按照最新的数据结构来
                PreferenceUtils.setData(PreferenceUtils.default_menu,null);
            }
        }
        if (appList != null ){
            if (!isexit(id)) {//不存在这个功能模块才添加
                appList.add(id);
                PreferenceUtils.setData(PreferenceUtils.default_menu, new Gson().toJson(appList));
            }
        }

    }

    /**
     * 获取被卸载的，本地功能模块，在应用商店，提供下载
     * @param context
     * @return
     */
    public static List<AppDataRoomInfo> getMenu_delete(Context context){
        List<AppDataRoomInfo> data = new ArrayList<>();

        List<AppMode> appModeList = getAppModes(context);
        Iterator item = appModeList.iterator();
        while (item.hasNext()) {
            AppMode model = (AppMode) item.next();
            if (isexit(model.getId())) {
                item.remove();
            }
        }
        for (int i = 0;i < appModeList.size();i++){
            AppMode appMode = appModeList.get(i);
            AppDataRoomInfo bean = new AppDataRoomInfo(appMode.getName(),appMode.getId()+"","");
            data.add(bean);
        }

        return data;
    }


    /**
     * 快捷菜单栏
     *这是所有菜单
     * @param context
     * @return
     */
    public static List<AppMode> getAppModes(Context context) {
        List<AppMode> mAppModes = new ArrayList<>();
        //mAppModes.add(new AppMode(AppFragment.ID_APP_STORE, context.getString(com.vercon.common.R.string.app_store)));
        mAppModes.add(new AppMode(AppFragment.ID_HEALTH, context.getString(com.vercon.common.R.string.main_health)));
        mAppModes.add(new AppMode(AppFragment.ID_MUSIC, context.getString(com.vercon.common.R.string.main_music),"com.tencent.qqmusiccar"));
        mAppModes.add(new AppMode(AppFragment.ID_VIDEO, context.getString(com.vercon.common.R.string.main_video),"com.kwai.tv.yst"));
        mAppModes.add(new AppMode(AppFragment.ID_FM, context.getString(com.vercon.common.R.string.main_fm),"com.ximalaya.ting.android.car"));
        mAppModes.add(new AppMode(AppFragment.ID_FLIQLO, context.getString(com.vercon.common.R.string.main_fliqlo)));
        mAppModes.add(new AppMode(AppFragment.ID_BRUSH, context.getString(com.vercon.common.R.string.main_brush)));
        mAppModes.add(new AppMode(AppFragment.ID_MESSAGE, context.getString(com.vercon.common.R.string.main_message)));
        mAppModes.add(new AppMode(AppFragment.ID_SETTING, context.getString(com.vercon.common.R.string.app_setting)));
        return mAppModes;
    }

    public static List<AppMode> getAppModesNew(Context context) {

        List<AppMode> appModeList = getAppModes(context);
        Iterator item = appModeList.iterator();
        while (item.hasNext()) {
            AppMode model = (AppMode) item.next();
            if (!isexit(model.getId())) {
                item.remove();
            }
        }
        LogUtils.e("getAppModesNew default_menu="+appModeList.toString());
        return appModeList;
    }

    /**
     * 判断自带的软件功能模块是否存在
     * @param data
     * @return
     */
    private static boolean isexit(int data){
        String menuData = PreferenceUtils.getData(PreferenceUtils.default_menu);
        LogUtils.e("default_menu="+menuData);
        List<Integer> appList = null;
        if (!TextUtils.isEmpty(menuData)) {
            try {
                appList = new Gson().fromJson(menuData, new TypeToken<List<Integer>>() {}.getType());
            }catch (Exception e){//以前版本，保存了本地数据，清空以前数据，按照最新的数据结构来
                LogUtils.e("e.getMessage()="+e.getMessage());
            }
        }
        if (appList != null && appList.size() > 0) {
            for (int i = 0; i < appList.size(); i++) {
                if (appList.get(i) == data ) {
                    return true;
                }
            }
        }else {
            return true;
        }
        return false;
    }



}
